package com.jumi.microservice.service.admin.afterseal;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jumi.microservice.common.constant.DelFlag;
import com.jumi.microservice.common.exception.BaseException;
import com.jumi.microservice.constant.refundgoods.RefundGoodsStatusEnum;
import com.jumi.microservice.entity.AfterSealOrder;
import com.jumi.microservice.entity.RefundGoods;
import com.jumi.microservice.mapper.AfterSealOrderMapper;
import com.jumi.microservice.mapper.RefundGoodsMapper;
import com.jumi.microservice.service.IAfterService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangjingdao
 */
@DubboService(interfaceClass = IAfterService.class)
public class AfterServiceImpl implements IAfterService {
    @Resource
    RefundGoodsMapper refundGoodsMapper;
    @Resource
    AfterSealOrderMapper afterSealOrderMapper;

    @Override
    public Map<String, BigDecimal> refundAmount(List<String> orderNo) {
        Map<String, BigDecimal> stringBigDecimalMap = new HashMap<>();
        QueryWrapper<RefundGoods> refundQueryWrapper = new QueryWrapper<>();
        refundQueryWrapper.in("original_order_no", orderNo);
        refundQueryWrapper.eq("del_flag", DelFlag.NORMAL);
        refundQueryWrapper.select("original_order_no, sum(refund_price) AS refund_price");
        refundQueryWrapper.eq("status", RefundGoodsStatusEnum.STATUS_COMPLETE.getCode());
        refundQueryWrapper.groupBy("original_order_no");
        List<RefundGoods> refundGoodss = refundGoodsMapper.selectList(refundQueryWrapper);
        refundGoodss.forEach(
                refundGoods -> {
                    stringBigDecimalMap.put(refundGoods.getOriginalOrderNo(), refundGoods.getRefundPrice());
                }
        );
        return stringBigDecimalMap;
    }

    @Override
    public String expressCode(String realLogisticsNo,String orderNo) {
        QueryWrapper<AfterSealOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("real_logistics_no", realLogisticsNo);
        queryWrapper.eq("order_no",orderNo).last("limit 1");
        AfterSealOrder afterSealOrder = afterSealOrderMapper.selectOne(queryWrapper);
        if (afterSealOrder==null) {
            throw new BaseException(500, "快递单号不存在！");
        }
        return afterSealOrder.getExpressCode();
    }

    @Override
    public String expressCompanyCode(String realLogisticsNo,String orderNo) {
        QueryWrapper<RefundGoods> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("logistics_no", realLogisticsNo);
        queryWrapper.eq("refund_order_no",orderNo).last("limit 1");
        RefundGoods refundGoods = refundGoodsMapper.selectOne(queryWrapper);
        if (refundGoods==null) {
            throw new BaseException(500, "快递单号不存在！");
        }
        return refundGoods.getExpressCode();
    }
}
